
/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.0.1
 *	LISENSE: FL87MPRYOA0R - http://www.flynax.com/license-agreement.html
 *	PRODUCT: Auto Classifieds
 *	DOMAIN: www.siyaradiali.com
 *	FILE: PHOTO_GALLERY.JS
 *
 *	This script is a commercial software and any kind of using it must be 
 *	coordinate with Flynax Owners Team and be agree to Flynax License Agreement
 *
 *	This block may not be removed from this file or any other files with out 
 *	permission of Flynax respective owners.
 *
 *	Copyrights Flynax Classifieds Software | 2012
 *	http://www.flynax.com/
 *
 ******************************************************************************/

$(document).ready(function(){
	/* enable fancybox */
	$('div.photos div.preview a, div#imgSource a').fancybox({
		titlePosition: 'over',
		centerOnScroll: true,
		scrolling: 'yes',
		removeFirst: true,
		customIndex: true
	});
	
	if ( $('div.slider').length <= 0 )
		return;
	
	var marginCss = rlLangDir == 'ltr' ? 'marginRight' : 'marginLeft';
		
	/* set sizes */
	var slider_width = $('div.photos div.slider').width();
	var item_margin = parseInt($('div.photos div.slider ul li:first img').css(marginCss).replace('px', ''));
	var item_border = 2;
	var item_width = $('div.photos div.slider ul li:first img').width() + item_margin + (item_border * 2);
	var items = $('div.photos div.slider ul li').length;
	var moving_width = item_width * items;
	var slider_position = 0;
	
	$('div.photos div.slider').width(slider_width);
	$('div.photos div.slider ul').width(moving_width).fadeIn();
	
	/* colculate pages */
	var items_per_slide = Math.floor((slider_width+item_margin)/item_width);
	var pages = items/items_per_slide;
	for ( var i=0; i<pages; i++ )
	{
		var attr_class = i == 0 ? ' class="active"' : '';
		$('div.photos div.navigation').append('<a'+attr_class+' id="imgnav_'+i+'" href="javascript:void(0)"><span>&nbsp;</span></a>');
	}
	
	var currentImage = 0;
	/* thumbnail click handler */
	$('div.photos div.slider ul li').click(function(){
		loadImage(this);
	});
	
	var loadImage = function(obj){
		var index = $(obj).index('div.photos div.slider ul li');
		if ( currentImage == index )
		{
			return;
		}
		currentImage = index;
		
		$(obj).children().after('<div class="img_loading"></div>');
		var width = $(obj).children().width();
		var height = $(obj).children().height();
		var border = $(obj).children()[0].clientLeft;
		var margin = (height + border) * -1;
		
		$(obj).children().next().width(width).height(height).css({opacity: 0.5, marginTop: margin, marginLeft: border});
		var photo_src = rlConfig['files_url'] + $(obj).attr('class');
		
//		$(obj).children().next().after('<div class="img_loading"></div>');
//		var style = $(obj).children().next().attr('style');
//		$(obj).children().next().next().attr('style', style).css('opacity', 0.6);
//		var photo_src = rlConfig['files_url'] + $(obj).attr('class');
		
		$.fancybox.setIndex(currentImage);
		
		var img = new Image();
		img.onload = function(){
			$('div.photos div.preview img').attr('src', photo_src);
//			var width = $('div.photos div.preview img').width();
//			var height = Math.round(img.height * width / img.width);
//			var border = $('div.photos div.preview img')[0].clientTop;
//			var new_margin = (height + border) * -1;
//			
//			$('div.photos div.preview img').next().width(width);
//			$('div.photos div.preview img').next().height(height).css({marginTop: new_margin});
			
			$(obj).children().next().fadeOut('normal', function(){
				$(this).remove();
			});
		}
		img.src = photo_src;
	}
	
	/* navigation click handler */
	$('div.photos div.navigation a').click(function(){
		var point = parseInt($(this).attr('id').split('_')[1]);
		
		if ( slider_position == point )
		{
			return;
		}
		
		slider_position = point;
		
		moveSlider();
	});
	$('div.photos div.next').click(function(){
		if ( slider_position + 1 < pages)
		{
			slider_position++;
			moveSlider();
		}
	});
	$('div.photos div.prev').click(function(){
		if ( slider_position )
		{
			slider_position--;
			moveSlider();
		}
	});
	
	var moveSlider = function(){
		var new_pos = (slider_position * item_width * items_per_slide) * -1;
		if ( rlLangDir == 'ltr' )
		{
			$('div.photos div.slider ul').animate({marginLeft: new_pos});
		}
		else
		{
			$('div.photos div.slider ul').animate({marginRight: new_pos});
		}
		$('div.photos div.navigation a').removeClass('active');
		$('div.photos div.navigation a:eq('+slider_position+')').addClass('active');
		
		var index = slider_position * items_per_slide;
		loadImage($('div.photos div.slider ul li:eq('+index+')'));
		
		if ( !slider_position )
		{
			$('div.photos div.prev').fadeOut();
		}
		else
		{
			$('div.photos div.prev').fadeIn();
		}
		
		if ( slider_position + 1 == Math.ceil(pages) )
		{
			$('div.photos div.next').fadeOut();
		}
		else
		{
			$('div.photos div.next').fadeIn();
		}
	};
});